
use num::complex::Complex;

fn test_range() {
    for i in 1..=5  {
        println!("{}",i);
    }
}
fn test_complex_num() {
    let a = Complex { re: 2.1, im: -1.2 };
    let b = Complex::new(11.1, 22.2);
    let result = a + b;

    println!("{} + {}i", result.re, result.im)
}
fn test_std_lib (){
    let z = '侯';
    // let za = 'a';
    println!("中文字符「侯」占{}个内存大小", std::mem::size_of_val(&z))
}
fn test_basic() {
    let b = false;
    if b == false {
        println!("有嘛用啊")
    }
    
    let mut v: Vec<i32> = Vec::new();
    v.push(11);

    let (a,c) = ('又', false);
    println!("a: {a}, c:{c  }")
}
fn test_arrow_fun(a: i16, b: i16) -> i16 {
    let aa = a*a;
    let bb = b*b;
    //aa +bb; //表达式
    //()单元类型
    return aa+bb; //或者直接用aa+bb, 不要带分号
}
fn test_previlige() {
    let s1 = String::from("world");
    //所有权
    // let s2 = s1;
    //println!("转让权限后的s1: {}", s1); //字符串所有权， s1在被赋予s2后就马上失效了

    let s2 = s1.clone();
    println!("test_previlige s2: {s2}");
}
fn test_str_previlige() {
    let a = "world";
    let b = a;
    println!("test_str_previlige ,a: {a}, b:{b}")
}
fn test_reference () {
    let a = 2;
    let b = &a;
    // println!("test_reference ,a: {a}, b:{}", *b);
    assert_eq!(2, *b); //right,*去地址中的值
    //assert_eq!(2, b); // error, b保存的是引用
}
fn calculate_length(s: &String) -> usize {
    s.len()
}
//不可变应用
fn test_immu_reference(){
    let s1 = String::from("hello,world");
    let len = calculate_length(&s1);
    println!("test_immu_reference, len: {len}")
}
fn change(some_string: &mut String) {
    some_string.push_str(", hyw");
    println!("changed result: {}", some_string);
}
//可变应用
fn test_mut_reference(){
    let mut s1 = String::from("hello");
    change(&mut s1);

    //注意：可变引用只能创建一次，下面的代码会报错
    // let r1 = &mut s1;
    // let r2 = &mut s1;
    
}
fn main() {
    test_basic();
    test_complex_num();
    test_range();
    test_std_lib();
    let cc = test_arrow_fun(2,3);
    println!("fn with return value: {}", cc);

    test_previlige();
    test_str_previlige();
    test_reference();
    test_immu_reference();
    test_mut_reference()
}
